<template>
<view>
	
	<uni-card @click="resetIndex">
	<uni-row>
		<uni-col :span="12">
			<view class="title2">{{ County }}
			<image src="../../static/icon/定位.svg" style="height: 20px;width: 20px;margin-bottom: -2px;"></image></view>
		</uni-col>	
		<uni-col :span="12">
			<view class="title3">{{ now.temp }}°C</view>
		</uni-col>	
		</uni-row>
</uni-card>
		
	
	
</view>
</template>

<script>
	const APIKEY = '7cc94edca9f74113adc43d1822d933c0' ;// 填入你申请的KEY
	export default {
	  data() {
	    return {
	    user: uni.getStorageSync('weather2024'),
	    currenttime:'',
	    location: '',
	    now: '',
	    hourly: '',
	    daily: '',
	    City: '',
	    County: ''
	     
	    }
	  },
	 created() {
	  },
	  onLoad() {
	  	this.getLocation();
	  	
	  	},
	  methods: {
		resetIndex(){
		uni.switchTab({
			url:'/pages/index/index'
		})	
			
		},
		  	/**
		  	 * 获取定位
		  	 */
		  	getLocation() {
		  		var that = this;
		  		uni.getLocation({
		  			type: 'gcj02',
		  			success(res) {
		  				that.location = res.longitude + ',' + res.latitude;
		  				that.getWeather();
		  				that.getCityByLoaction();
		  			},
		  			fail(err) {
		  				uni.showModal({
		  					title: '获取定位信息失败',
		  					content: '为了给您提供准确的天气预报服务,请在设置中授权【位置信息】',
		  					success(mRes) {
		  						if (mRes.confirm) {
		  							uni.openSetting({
		  								success: function (data) {
		  									if (data.authSetting['scope.userLocation'] === true) {
		  										uni.showToast({
		  											title: '授权成功',
		  											icon: 'success',
		  											duration: 1000
		  										});
		  										that.getLocation();
		  									} else {
		  										uni.showToast({
		  											title: '授权失败',
		  											icon: 'none',
		  											duration: 1000
		  										});
		  										that.location = '地理位置';
		  										that.getWeather();
		  										that.getCityByLoaction();
		  									}
		  								},
		  								fail(err) {
		  									console.log(err);
		  									uni.showToast({
		  										title: '唤起设置页失败，请手动打开',
		  										icon: 'none',
		  										duration: 1000
		  									});
		  									that.location = '地理位置';
		  									that.getWeather();
		  									that.getCityByLoaction();
		  								}
		  							});
		  						} else if (mRes.cancel) {
		  							that.location = '地理位置';
		  							that.getWeather();
		  							that.getCityByLoaction();
		  						}
		  					}
		  				});
		  			}
		  		});
		  	},
		  	/**
		  	 * 根据坐标获取城市信息
		  	 */
		  	getCityByLoaction() {
		  		var that = this;
		  		uni.request({
		  			url:
		  				'https://geoapi.qweather.com/v2/city/lookup?key=' +
		  				APIKEY +
		  				'&location=' +
		  				that.location,
		  			success(result) {
		  				var res = result.data;
		  				if (res.code == '200') {
		  					var data = res.location[0];
		  					that.City = data.adm2;
		  					that.County = data.name;
		  				} else {
		  					uni.showToast({
		  						title: '获取城市信息失败',
		  						icon: 'none'
		  					});
		  				}
		  			}
		  		});
		  	},
		  	/**
		  	 * 获取天气
		  	 */
		  	getWeather() {
		  		var that = this;
		  		uni.showLoading({
		  			title: '加载中'
		  		});
		  		uni.request({
		  			url:
		  				'https://devapi.qweather.com/v7/weather/now?key=' +
		  				APIKEY +
		  				'&location=' +
		  				that.location,
		  			success(result) {
		  				var res = result.data;
		  				that.now = res.now;
		  			}
		  		});
		  		uni.request({
		  			url:
		  				'https://devapi.qweather.com/v7/weather/24h?key=' +
		  				APIKEY +
		  				'&location=' +
		  				that.location,
		  			success(result) {
		  				var res = result.data;
		  				res.hourly.forEach(function (item) {
		  					item.time = that.formatTime(new Date(item.fxTime)).hourly;
		  				});
		  				that.hourly = res.hourly;
		  			}
		  		});
		  		uni.request({
		  			url:
		  				'https://devapi.qweather.com/v7/weather/7d?key=' +
		  				APIKEY +
		  				'&location=' +
		  				that.location,
		  			success(result) {
		  				var res = result.data;
		  				res.daily.forEach(function (item) {
		  					item.date = that.formatTime(new Date(item.fxDate)).daily;
		  					item.dateToString = that.formatTime(new Date(item.fxDate)).dailyToString;
		  				});
		  				that.daily = res.daily;
		  				uni.hideLoading();
		  			}
		  		});
		  	},
		  	// 格式时间
		  	formatTime(date) {
		  		const year = date.getFullYear();
		  		const month = date.getMonth() + 1;
		  		const day = date.getDate();
		  		const hour = date.getHours();
		  		const minute = date.getMinutes();
		  		const second = date.getSeconds();
		  		const weekArray = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
		  		const isToday = date.setHours(0, 0, 0, 0) == new Date().setHours(0, 0, 0, 0);
		  		return {
		  			hourly: [hour, minute].map(this.formatNumber).join(':'),
		  			daily: [month, day].map(this.formatNumber).join('-'),
		  			dailyToString: isToday ? '今天' : weekArray[date.getDay()]
		  		};
		  	},
		  	// 补零
		  	formatNumber(n) {
		  		n = n.toString();
		  		return n[1] ? n : '0' + n;
		  	}
		  
		
	    
	  }
	}
</script>

<style>
	.title2{
		 
				font-family: "Brush Script MT", "Lucida Handwriting", Cursive;
				font-weight: bolder;
				color: black;
				font-size: 22px;
				
				justify-content: left;
				align-items: center;
		}
		.title3{
			 
					font-family:  "Times New Roman", Times, serif;
					font-weight: bolder;
					color: black;
					font-size: 28px;
					display: flex;
					justify-content: center;
					align-items: center;
						
						
			}
		
	
</style>